﻿using cnFederal.FaCode;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cnFederal.Fa_Forms
{
    public partial class EditSelectedUser : System.Web.UI.Page
    {
        CnfiUser _usr;
        CultureInfo _ci;
        public EditSelectedUser() 
        {
            _ci = new CultureInfo("nb-NO");
            CnfImages.Load();
            CnfiUsers.Load();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int id = -1;
           
            string query = Request.QueryString["Id"];
            if (string.IsNullOrWhiteSpace(query))
                txtRegDate.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;

            if (!string.IsNullOrWhiteSpace(query))
            {

                try
                {
                    id = int.Parse(query);
                }
                catch (Exception ex) { }
                if (id != -1)
                    Load(id);

            }

            MainTable.Visible = UserIdentity.Autenticated;
        }


        public void Load(int id)
        {
            _usr = CnfiUsers.GetUserById(id);
            if (!IsPostBack && _usr != null)
            {
                txtRegDate.Text = _usr.DateRegistered.Year + "-" + _usr.DateRegistered.Month + "-" + _usr.DateRegistered.Day;
                txtUsername.Text = _usr.Username;
                txtName.Text = _usr.Name;
                txtEmail.Text = _usr.Email;
                txtTlf.Text = _usr.Tlf;
                txtMobile.Text = _usr.Mobile;
                txtMacId.Text = _usr.MacId;
                txtIpadresse.Text = _usr.Ipadresse;
                //TxtPassword.Text = _usr.Password; 
                ChkAdmin.Checked = _usr.IsAdmin;
                ChkAuthor.Checked = _usr.IsAuthor;
            }
        }
        protected void txtRegDate_TextChanged(object sender, EventArgs e)
        {



        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
            if (_usr == null)
                CreateNew();
            else
                Update();
        }

        private void Update()
        {
            _usr.Username = txtUsername.Text;
            _usr.Name = txtName.Text;
            _usr.Email = txtEmail.Text;
            _usr.Tlf = txtTlf.Text;
            _usr.Mobile = txtMobile.Text;
            _usr.IsAuthor = ChkAuthor.Checked;
            _usr.IsAdmin = ChkAdmin.Checked;
            _usr.MacId = txtMacId.Text;
            //_usr.Password = TxtPassword.Text;
            _usr.Ipadresse = txtIpadresse.Text;
            _usr.DateRegistered = Convert.ToDateTime(txtRegDate.Text);
            CnfiUsers.Save(_usr);
        }

        private void CreateNew()
        {
            _usr = new CnfiUser();
            _usr.Username = txtUsername.Text;
            _usr.Name = txtName.Text;
            _usr.Email = txtEmail.Text;
            _usr.Tlf = txtTlf.Text;
            _usr.Mobile = txtMobile.Text;
            _usr.IsAuthor = ChkAuthor.Checked;
            _usr.IsAdmin = ChkAdmin.Checked;
            _usr.MacId = txtMacId.Text;
            //_usr.Password = TxtPassword.Text;
            _usr.Ipadresse = txtIpadresse.Text;
            _usr.DateRegistered = Convert.ToDateTime(txtRegDate.Text);
            CnfiUsers.CreateNew(_usr);
        }


    }
}